let app = getApp();
let API_URL = app.globalData.API_URL;
import { getHomeVideo } from "../../../request/api";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        videoList: [],
        changeIndex: 0,
        waiting: true,
        lestTime: 0, // 历时

        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',

        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',

        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',

        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px',

        // 分页
        pageNo: 1,
        totalPage: 0,
    },

    // 获取内容
    getPost() {
        let data = { pageNo: this.data.pageNo, pageSize: 5 }
        getHomeVideo(res => {
            if (res.statusCode == 200) {
                this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage : res.data.totalPage })
            }
        }, data);
    },

    // 返回
    previous() {
        wx.navigateBack({
            delta: 1,
            fail: err => {
                console.log("err: ", err);
                wx.navigateTo({
                  url: '/pages/index/index',
                })
            }
        })
    },

    // 防止速滑
    videoLoaded(e) {
        // console.log("videoLoaded!", e);
        this.setData({ waiting: false })
    },

    //划动切换
    slide(e) {
        this.setData({
            changeIndex: e.detail.current,
            waiting: true
        })
        
        if (this.data.videoList.length - e.detail.current == 2) {
            if (this.data.pageNo == this.data.totalPage) return
            let pageNo = this.data.pageNo += 1
            this.setData({ pageNo: pageNo })
            this.getPost();
        }
    },

    // 前往个人视频页面
    videoDetailed(e){
        let item = e.currentTarget.dataset.item.user;
        let json = JSON.stringify(item);

        wx.navigateTo({
            url: '/pages/home/video/videoDetailed/videoDetailed?json=' + json
        });
    },

    // 点击喜欢
    onClickLike(e) {
        // 当前时间
        let currentTime = new Date().valueOf()

        // 首次请求
        if (this.data.lestTime - currentTime == 0) {
            this.setData({ lestTime: currentTime });
            this.likeFun(e);
        }

        // 次请求
        if (currentTime - this.data.lestTime > 3000) {
            this.setData({ lestTime: currentTime });
            this.likeFun(e);
        }
    },

    // 检查登录状态后调用喜欢方法
    likeFun(e){
        let promise = new Promise(function (resolve, rej) {
            app.login(res => {
                resolve(res)
            })
        })

        promise.then(
            resolve => {
                let videoId = e.currentTarget.dataset.item.id
                let item = this.data.videoList[e.currentTarget.dataset.index]
                item.isLike = !item.isLike
                item.favorite_count = item.favorite_count + (item.isLike ? 1 : -1)
                this.setData({ videoList: this.data.videoList })
                let data = { videoId }

                wx.request({
                    url: `${API_URL}/videoFavorite`,
                    header: app.header(),
                    data: data,
                    method: 'post',
                    success: function (res) {
                        if (res.statusCode == 200) {

                        }
                    }
                })
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})